<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="../../js/vue.js"></script>
    <script src="../../js/vue-router.js"></script>
</head>

<body>



    <div id="app">
        <router-link to="/user/1">User1</router-link>
        <router-link to="/user/2">User2</router-link>
        <router-link to="/user/3">User3</router-link>
        <router-link to="/register">Register</router-link>

        <router-view></router-view>
    </div>

    <script>
        const User = {
            props: ['id', 'name', 'age'],
            template: `
            <div>
                <h1>user{{ id }}组件信息--name:{{ name }}---age:{{ age }}</h1>
                <button @click='goRegister'>跳转到注册页面</button>    
            </div>
            `,
            methods: {
                goRegister() {
                    this.$router.push('/register')
                }
            }
        }
        const Register = {
            template: `
            <div>
                <h1>user{{ id }}组件信息--name:{{ name }}---age:{{ age }}</h1>
                <button @click='goback'>后退</button>    
            </div>
            `,
            methods: {
                goback(){
                    router.go(-1)
                }
            }
        }

        var router = new VueRouter({
            routes: [
                {
                    name: 'user',
                    path: '/user/:id',
                    component: User,
                    props: function (route) {
                        return {
                            age: 128,
                            name: 'lisi',
                            id: route.params.id
                        }
                    }
                },
                { path: '/register', component: Register }
            ]
        })

        var vm = new Vue({
            el: '#app',
            router: router
        })
    </script>
</body>

</html>